#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>

#include "Game.h"

void		Game::cam_move(int x, int y)
{
  x += this->_cam.x;
  y += this->_cam.y;
  if (x >= 0 && x < _xdim && y >= 0 && y < _ydim)
    {
      this->_cam.x = x;
      this->_cam.y = y;
    }
  else
    std::cout << "Cam blocked at " << this->_cam.x << " " << this->_cam.y << std::endl;
}

void		Game::applyCameraZoom(SDL_Rect& cursorPos)
{
  this->_cam.x = cursorPos.x + 1 - (_xdim / this->_zoom) / 2;
  this->_cam.y = cursorPos.y + 1 - (_ydim / this->_zoom) / 2;
  if (this->_cam.x < 0)
    this->_cam.x = 0;
  if (this->_cam.y < 0)
    this->_cam.y = 0;
  if (this->_cam.x > _xdim - (_xdim / this->_zoom))
    this->_cam.x = _xdim - (_xdim / this->_zoom);
  if (this->_cam.y > _ydim - (_ydim / this->_zoom))
    this->_cam.y = _ydim - (_ydim / this->_zoom);
}

void		Game::updateMousePos()
{
  int	x;
  int	y;
  
  SDL_GetMouseState(&x, &y);
  this->_mouse.x = (x / this->_zoom) + this->_cam.x;
  this->_mouse.y = (y / this->_zoom) + this->_cam.y;
}

int		Game::manage_keyboard()
{
  Uint8			*keystate;

  keystate = SDL_GetKeyState(NULL);
  if (keystate[SDLK_UP])
    cam_move(0, -1);
  if (keystate[SDLK_DOWN])
    cam_move(0, 1);
  if (keystate[SDLK_LEFT])
    cam_move(-1, 0);
  if (keystate[SDLK_RIGHT])
    cam_move(1, 0);
  if (keystate[SDLK_ESCAPE])
    return (0);
  if (keystate[SDLK_PAGEUP])
    {
      ++this->_zoom;
      this->applyCameraZoom(this->_mouse);
    }
  if (keystate[SDLK_PAGEDOWN])
    {
      --this->_zoom;
      if (this->_zoom < 1)
	this->_zoom = 1;
      this->applyCameraZoom(this->_mouse);
    }
  if (keystate[SDLK_s])
    sleep(1);
  if (keystate[SDLK_p])
    {
      if (this->_paused)
	_paused = false;
      else
	_paused = true;
    }
  if (keystate[SDLK_RETURN])
    {
      if (_edges)
	this->_edges = 0;
      else
	this->_edges = 1;
    }
  return (1);
}

void		Game::manage_mouse(SDL_Event *event)
{
  /* Deplacement mouse drag
  cam_move(-1 * (event->button.x - this->_mouse.x),
	   -1 * (event->button.y - this->_mouse.y));
  */
  this->_mouse.x = event->button.x;
  this->_mouse.y = event->button.y;
}

int		Game::manage_keys()
{
  SDL_Event		event;

  if (SDL_PollEvent(&event))
    {
      this->updateMousePos();
      if (event.type == SDL_QUIT)
	return (0);
      else if (event.type == SDL_KEYDOWN)
	{
	  if (!manage_keyboard())
	    return (0);
	}
      else if (event.type == SDL_MOUSEBUTTONDOWN)
	{
	  if (event.button.button == SDL_BUTTON_LEFT)
	    this->_clicked = 1;
	  else if (event.button.button == SDL_BUTTON_WHEELUP)
	    {
	      ++this->_zoom;
	      this->applyCameraZoom(this->_mouse);
	    }
	  else if (event.button.button == SDL_BUTTON_WHEELDOWN)
	    {
	      --this->_zoom;
	      if (this->_zoom < 1)
		this->_zoom = 1;
	      this->applyCameraZoom(this->_mouse);
	    }
	}
      else if (event.type == SDL_MOUSEBUTTONUP)
	this->_clicked = 0;
      //      else if (event.type == SDL_MOUSEMOTION && this->_clicked)
      //	manage_mouse(&event);
    }
  return (1);
}

void		Game::ini_keys()
{
  SDL_EnableKeyRepeat(100, 20);
}
